package bold.module.kafka;

import lombok.extern.log4j.Log4j2;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;
import java.util.concurrent.TimeUnit;

@Log4j2
public class Producer extends Thread {
    private final KafkaProducer<String, String> producer;   //生产者

    public Producer() {
        Properties props = new Properties();
        props.put("bootstrap.servers", Config.brokerList);
        props.put("acks", "all");
        props.put("retries", 0);
        props.put("batch.size", 16384);
        props.put("linger.ms", 1);
        props.put("buffer.memory", 33554432);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        producer = new KafkaProducer<>(props);
    }

    @Override
    public void run() {
        int num = 1;
        while (true) {
            log.debug("------------发送：" + num);
            ProducerRecord record = new ProducerRecord<>(Config.topic, String.valueOf(num++));
            producer.send(record);
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
            }
        }
    }

    public static void main(String[] args) {
        new Producer().start();
    }
}